package com.vids.task;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import com.vids.constant.GlobalConstant;
import com.vids.constant.Urlconstant;
import com.vids.constant.VIIDRespEnum;
import com.vids.model.entity.Register;
import com.vids.model.entity.VIIDServer;
import com.vids.model.response.ResponseStatus;
import com.vids.mapper.VIIDServerMapper;
import com.vids.service.IBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@Component
public class SyncViidServerStatus implements SyncStatus {

    @Resource
    VIIDServerMapper viidServerMapper;

    @Resource
    IBaseService iBaseService;

    @Autowired
    RedisTemplate<String, String> redisTemplate;

    @Override
    public ResponseStatus register(Register vo) {
		ResponseStatus status = new ResponseStatus(Urlconstant.URL_REGISTER, VIIDRespEnum.OK.getCode(), new Date());
		String viidServerID = "1";
		try {
			if (ObjectUtil.isNull(vo)) {
				status.setStatusCode(VIIDRespEnum.INVALID_OPERATION.getCode());
				status.setStatusString("联网注册对象ID为空！");
				return status;
			}
			viidServerID = vo.getDeviceID();
			// TODO:后面加入设备，设备系统，应用平台，分析系统注册。
			// 视图库注册
			VIIDServer viidServer = viidServerMapper.findByVIIDServerID(viidServerID);
			if (ObjectUtil.isNull(viidServer)) {
				status.setStatusCode(VIIDRespEnum.INVALID_OPERATION.getCode());
				status.setStatusString("需要添加联网对象！");
				return status;
			}

			//status 表也要更新  已经注册的直接返回注册成功，而不是报错
			if (viidServer.getIsOnline().equals(GlobalConstant.REGISTER_CODE)) {
				String deviceStr = redisTemplate.opsForValue().get(GlobalConstant.UNREGIST_DEVICE);
				if (CharSequenceUtil.isNotBlank(deviceStr)) {
					redisTemplate.opsForValue().set(GlobalConstant.UNREGIST_DEVICE, deviceStr.replace(viidServerID + ",", ""));
				}
//				status.setId("42100000125030000041");
				status.setStatusCode(VIIDRespEnum.OK.getCode());
				status.setId(viidServerID);
				//status.setStatusCode(VIIDRespEnum.INVALID_OPERATION.getCode());
				//status.setStatusString("联网对象已经注册！");
				log.info("============》联网服务已经注册成功 viidServerID="+viidServerID);
				status.setStatusString("联网服务注册成功！");
				log.info("联网服务已经注册成功！");
				int i = iBaseService.updateAndInsertSystemStatus(GlobalConstant.REGISTER_CODE, viidServerID);
				return status;
			}

			viidServer.setIsOnline("1");
			viidServer.setLastOnlineTime(new Date());

			int i = iBaseService.updateAndInsertSystemStatus(GlobalConstant.REGISTER_CODE, viidServerID);
			int j = viidServerMapper.updateById(viidServer);

			if (j > 0) {
				String deviceStr = redisTemplate.opsForValue().get(GlobalConstant.UNREGIST_DEVICE);
				if (CharSequenceUtil.isNotBlank(deviceStr)) {
					redisTemplate.opsForValue().set(GlobalConstant.UNREGIST_DEVICE, deviceStr.replace(viidServerID + ",", ""));
				}
				status.setId(viidServerID);
				status.setStatusString("联网服务注册成功！");

				log.info("============》register viidServerID="+viidServerID);
				log.info("============》联网服务成功注册");
				return status;
			}
		} catch (Exception e) {
			e.printStackTrace();
			status.setId(viidServerID);
			status.setStatusCode(VIIDRespEnum.DEVICE_ERROR.getCode());
			status.setStatusString(e.getMessage());
			return status;
		}
		return null;
    }

    @Override
    public void syncUpdateStatus() {
		ResponseStatus status = new ResponseStatus(Urlconstant.URL_REGISTER, VIIDRespEnum.OK.getCode(), new Date());
		log.info(">>>>>>>>> 更新VIIDServer 状态 >>>>>>>>>>>   间隔时间为三秒");
		// 刷离线
		iBaseService.updateStatusByDate();
		// 根据status 表 刷新状态
		viidServerMapper.updateIsOnlineStatuts();
    }

    @Override
    public ResponseStatus unRegister(Register vo) {
		ResponseStatus status = new ResponseStatus(Urlconstant.URL_REGISTER, VIIDRespEnum.OK.getCode(), new Date());
		String viidServerID = "1";
		try {
			if (ObjectUtil.isNull(vo)) {
				status.setStatusCode(VIIDRespEnum.INVALID_OPERATION.getCode());
				status.setStatusString("联网注销对象ID为空！");
				return status;
			}

			viidServerID = vo.getDeviceID();

			// 视图库保活
			VIIDServer viidServer = viidServerMapper.findByVIIDServerID(viidServerID);
			if (ObjectUtil.isNull(viidServer)) {
				status.setStatusCode(VIIDRespEnum.INVALID_OPERATION.getCode());
				status.setStatusString("需要添加联网对象！");
				return status;
			}
			viidServer.setIsOnline("2");
			int i = iBaseService.updateAndInsertSystemStatus(GlobalConstant.UNREGISTER_CODE, viidServerID);
			int j = viidServerMapper.updateById(viidServer);
			if (j > 0) {
				String deviceStr = redisTemplate.opsForValue().get(GlobalConstant.UNREGIST_DEVICE);
				if (CharSequenceUtil.isNotBlank(deviceStr)) {
					redisTemplate.opsForValue().set(GlobalConstant.UNREGIST_DEVICE, deviceStr + viidServerID + ",");
				} else {
					redisTemplate.opsForValue().set(GlobalConstant.UNREGIST_DEVICE, viidServerID + ",");
				}
				status.setId(viidServerID);
				status.setStatusString("联网服务注销成功！");
				return status;
			}

		} catch (Exception e) {
			e.printStackTrace();
			status.setId(viidServerID);
			status.setStatusCode(VIIDRespEnum.DEVICE_ERROR.getCode());
			status.setStatusString(e.getMessage());
			return status;
		}
		return status;
    }

    @Override
    public ResponseStatus keepLive(Register vo) {
	ResponseStatus status = new ResponseStatus(Urlconstant.URL_KEEPALIVE, VIIDRespEnum.OK.getCode(), new Date());
	String viidServerID = "1";
	try {
	    if (ObjectUtil.isNull(vo)) {
		status.setStatusCode(VIIDRespEnum.INVALID_OPERATION.getCode());
		status.setStatusString("联网保活对象ID为空！");
		return status;
	    }

	    viidServerID = vo.getDeviceID();

	    // 视图库保活
	    VIIDServer viidServer = viidServerMapper.findByVIIDServerID(viidServerID);
	    if (ObjectUtil.isNull(viidServer)) {
		status.setStatusCode(VIIDRespEnum.INVALID_OPERATION.getCode());
		status.setStatusString("需要添加联网对象！");
		return status;
	    }
	    String deviceStr = redisTemplate.opsForValue().get(GlobalConstant.UNREGIST_DEVICE);
	    if (CharSequenceUtil.isNotBlank(deviceStr) && deviceStr.indexOf(viidServerID) != -1) {
		status.setId(viidServerID);
		status.setStatusCode(VIIDRespEnum.DEVICE_ERROR.getCode());
		status.setStatusString("联网服务需要先注册");
		return status;
	    }
	    viidServer.setIsOnline("1");
	    int i = iBaseService.updateAndInsertSystemStatus(GlobalConstant.REGISTER_CODE, viidServerID);
	    // int j = viidServerMapper.updateById(viidServer);
	    if (i > 0) {
			status.setId(viidServerID);
			status.setStatusString("联网服务保活成功！");
			log.info("联网服务保活成功！");
			return status;
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    status.setId(viidServerID);
	    status.setStatusCode(VIIDRespEnum.DEVICE_ERROR.getCode());
	    status.setStatusString(e.getMessage());
	    return status;
	}
	return status;
    }

    @Override
    public void time() {

    }

}
